Tute (Week 8)
Table of Contents
1. Data Types
1.1. Converting from Haskell
Use MinHS type constructors \(+\), \(\times\) and \(\mathbf{rec}\) to describe types isomorphic to each of the following Haskell types. Give example terms for each type.
data Direction = East | West | North | South data Foo = Foo Int Bool Int data Tree = Node Tree Tree | Leaf
1.2. Recursive Types
Give an example MinHS term for each of the following types (if such a term exists), and derive the typing judgement for that term.
- \(\mathbf{rec}\ t.\ (\mathtt{Int} + t)\)
- \(\mathbf{rec}\ t.\ (\mathtt{Int} \times \mathtt{Bool})\)
- \(\mathbf{rec}\ t.\ (\mathtt{Int} \times t)\)
1.3. Type Isomorphisms
Which of the following types are isomorphic to each other (assuming a strict semantics)?
- \(\textbf{rec}\ t.\ t \times t\)
- \(\textbf{0}\)
- \(\textbf{1}\)
- \(\textbf{1} \times \textbf{0}\)
- \(\textbf{rec}\ t.\ t \times \textbf{1}\)
- \(\textbf{rec}\ t.\ t + \textbf{1}\)
1.4. Typing Terms
For each of the following terms, give a possible type for the term (there may be more than one type).
- \((\mathsf{InL}\ \mathsf{True})\)
- \((\mathsf{InR}\ \mathsf{True})\)
- \((\mathsf{InL}\ (\mathsf{InL}\ \mathsf{True}))\)
- \((\mathsf{roll}\ (\mathsf{InR}\ \mathsf{True}))\)
- \((\texttt{()}, (\mathsf{InL}\ \texttt{()}))\)
1.5. Curry-Howard
1.5.1. Proof Witnesses
For which of the following types can you write a terminating, total (i.e., not returning error, undefined or causing other runtime errors, but a value of the result type) MinHs function? Try to answer this question without trying to implement the function.
- \((a \rightarrow b) \rightarrow (b \rightarrow c) \rightarrow (a \rightarrow c)\)
- \(((a \times b) \rightarrow c) \rightarrow (a \rightarrow c)\)
- \((a \rightarrow c) \rightarrow ((a \times b) \rightarrow c)\)
What proposition does each of these types correspond to?
1.5.2. Proving a proposition
Write a program which is a proof of the symmetry of disjunction, i.e. \[A \lor B \Rightarrow B \lor A\].
1.5.3. Constructivity
Lambda calculus is said to correspond to constructive logic. What is meant by constructive here?
2. Polymorphism
2.1. Quantifier Positions
Explain the difference between a function of type \(\forall a.\ a \rightarrow a\) and \((\forall a.\ a) \rightarrow (\forall a.\ a)\).
2.2. Parametricity
What can we conclude about the following functions solely from looking at their type signature?
- \(f_1 : \forall a.\ [a] \rightarrow a\)
- \(f_2 : \forall a.\ \forall b.\ [a] \rightarrow [b]\)
- \(f_3 : \forall a.\ \forall b.\ [a] \rightarrow b\)
- \(f_4 : [\texttt{Int}] \rightarrow [\texttt{Int}]\)